#include "starship.h"
#include <qmath.h>
Starship::Starship(qint32 x, qint32 y)
{
    QObject();
    m_pixmap = QPixmap("anakin_starfighter_6812.png");
    m_x = x;
    m_y = y;

    //connect(this, SIGNAL(transmitGeoData(qint32,qint32,double)),bullet,SLOT(receiptGeoData(qint32,qint32,double)));

}
void Starship::draw(QPainter *painter)
{
    rotate(m_curX,m_curY);

    //emit transmitGeoData(m_x,m_y,m_angleInRads);
    painter->translate(m_x,m_y);
    painter->rotate(m_angle);
    painter->setPen(Qt::black);
    painter->drawPixmap(- m_pixmap.width() / 2.0, -m_pixmap.height() / 2.0, m_pixmap);
    painter->resetTransform();
    emit transmitShipInfo(m_x,m_y,m_angleInRads);
    //painter->end();
}
void Starship::updateLocation(qint32 x, qint32 y)
{
    m_x += x;
    m_y += y;
    if (m_x>500)
        m_x=500;
    else if (m_x<0)
        m_x=0;
    if (m_y>=500)
        m_y=499;
    else if (m_y<0)
        m_y=0;
}
void Starship::receiptCursor(qint32 x, qint32 y)
{
    m_curX = x;
    m_curY = y;
}
void Starship::rotate(qint32 toX, qint32 toY)
{
    m_angleInRads = atan2(toY - m_y, toX - m_x);
    m_angle = m_angleInRads <= 0.0
    ? m_angleInRads * 180.0 / 3.14159265359
    : m_angleInRads * 180.0 / 3.14159265359 - 360.0;
}
